<template>
  <Dialog
    :title="dialogTitle"
    v-model="dialogVisible"
    width="70%"
    center
    align-centers
    :fullscreen="false"
    @close="cancel"
  >
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="组织机构" prop="projectId">
          <el-tree-select
            v-model="queryParams.projectId"
            :data="deptList"
            :props="defaultProps"
            check-strictly
            node-key="id"
            placeholder="请选择组织机构"
            class="!w-240px"
            filterable
            clearable
          />
        </el-form-item>
        <el-form-item label="存放地点" prop="storageLocation">
          <el-input
            v-model="queryParams.storageLocation"
            placeholder="请输入存放地点"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
          <el-button
            type="primary"
            plain
            @click="openForm('create')"
            v-hasPermi="['rep:storage-location:create']"
          >
            <Icon icon="ep:plus" class="mr-5px" /> 新增
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        height="300"
      >
        <el-table-column label="序号" type="index" width="60" />
        <el-table-column label="所属公司" align="center" prop="parentDeptName" />
        <el-table-column label="所属项目" align="center" prop="projectName" />
        <el-table-column label="存放地点" align="center" prop="storageLocation" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column
          label="更新时间"
          align="center"
          prop="updateTime"
          :formatter="dateFormatter"
          width="180px"
        />
        <el-table-column label="操作" align="center" min-width="120px">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="openForm('update', scope.row.id)"
              v-hasPermi="['rep:storage-location:update']"
            >
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </Dialog>

  <!-- 表单弹窗：添加/修改 -->
  <StorageLocationForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { StorageLocationApi, StorageLocationVO } from '@/api/rep/ledger/storagelocation'
import StorageLocationForm from './StorageLocationForm.vue'
import { defaultProps, handleTree } from '@/utils/tree'
import * as DeptApi from '@/api/system/dept'

/** 台账-存放地点 列表 */
defineOptions({ name: 'StorageLocationList' })

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('存放地点') // 弹窗的标题
const loading = ref(true) // 列表的加载中
const list = ref<StorageLocationVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  storageLocation: undefined,
  status: undefined,
  projectId: undefined
})
const queryFormRef = ref() // 搜索的表单
const deptList = ref<Tree[]>([]) // 树形结构

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await StorageLocationApi.getStorageLocationPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

const getDeptList = async () => {
  // 加载部门树
  deptList.value = handleTree(await DeptApi.getSimpleDeptList())
}

/** 打开弹窗 */
const open = () => {
  dialogVisible.value = true
  getList()
  getDeptList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

const cancel = () => {
  // 发送操作取消的事件
  emit('success')
  dialogVisible.value = false
}
</script>
